package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
      @Autowired
    private IUserService service;
    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest req){
        UserBean user= service.land(userName,pwd);
        if (user!=null){
            req.getSession().setAttribute("loginUser",user);
            return "ok";
        }
        return "no";
    }
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req){
        return (UserBean) req.getSession().getAttribute("loginUser");
    }
    @RequestMapping("findByItem")
    public IPage<UserBean> findByItem(Integer pageNo, String userName){
       return service.findByItem(pageNo,userName);
    }
    @RequestMapping("add")
    public String add(String userName, String name,Integer[] roleId){
        UserBean user = new UserBean(userName,name);
        if (service.add(user,roleId)){
            return "ok";
        }
        return "no";
    }
     @RequestMapping("del")
    public String del(Integer id, HttpServletRequest req) {
        UserBean user = (UserBean)req.getSession().getAttribute("loginUser");
        if (service.del(id,user)){
            return "ok";
        }
        return "no";
    }

    @RequestMapping("findById")
    public UserBean findById(Integer id) {
        return service.findById(id);
    }
    @RequestMapping("update")
    public String update(Integer userId,Integer[] roleId){
        service.updateRol(userId,roleId);
        return "ok";
    }
   @RequestMapping("updatePw")
    public String updatePw(Integer userId,String pwd){
        service.updatePwd(userId,pwd);
        return "ok";
    }
}
